#!/usr/bin/env python2

# -*- coding: utf-8 -*-
import os
from app import create_app, db
from app.models import User, Role, Star, Job, \
    Source
from flask_script import Manager, Shell
from flask_migrate import Migrate, MigrateCommand

from flask import g, session, request, url_for, redirect

app = create_app(os.getenv('FLASK_CONFIG')  or 'default')
manager = Manager(app)
migrate = Migrate(app, db)

# @app.before_request
# def before_request():
#     g.user = None
#     if 'login_user' in session:
#         g.user = User.query.filter(User.id == session['login_user']).first()
#     elif request.path != '/login':
#         app.logger.debug(request.path)
#         return redirect(url_for('main.login'))


def make_shell_context():
    return dict(app=app, db=db, User=User, Role=Role, Star=Star, \
                Job=Job, Source=Source)

manager.add_command("shell", Shell(make_context=make_shell_context))
manager.add_command('db', MigrateCommand)

if __name__ == '__main__':
    manager.run()